package com.example.demo2.test.websocket;

import com.alibaba.fastjson.JSONObject;
import com.example.demo2.test.Util.DataFormatUtil;
import com.example.demo2.test.factory.ConnectionFactory;
import com.example.demo2.test.pong.HuoBiPong;
import com.example.demo2.test.service.HuobiService;
import com.example.demo2.test.service.OkService;
import lombok.Data;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.WebSocket;
import okhttp3.WebSocketListener;
import okio.ByteString;

import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 火币websocket连接管理类
 * @author yusong
 * @date 
 */
@Data
@Slf4j
public class HuobiWebSocket extends WebSocketListener{

//	private String uri = "wss://api.huobi.pro/ws";
//	private String uri = "wss://api.huobi.tj/ws";
	private HuobiService huobiService;
	//websocket连接
	private WebSocket webSocket;
	//定义一个request信息
	private Request okhttpRequest;
	//定义一个私有的构造函数 防止外部创建
	private HuobiWebSocket() {}
	/**
	 * 创建websocket连接
	 * @return
	 */
	public static HuobiWebSocket createConnectionManage(HuobiService huobiService, String uri) {
		HuobiWebSocket huobiWebSocket = new HuobiWebSocket();
		huobiWebSocket.setHuobiService(huobiService);
		Request request = new Request.Builder().url(uri).build();
		huobiWebSocket.setOkhttpRequest(request);
		huobiWebSocket.connect();
		return huobiWebSocket;
	}
	
	//开启一个1个线程的连接池 用于检测连接是否正常
	static ExecutorService pool = Executors.newFixedThreadPool(1);
	static HuoBiPong pong = null;
	/**
	 * 提供一个非静态的方法 创建webSocket
	 */
	private void connect() {
		webSocket = ConnectionFactory.createWebSocket(okhttpRequest, this);
		pong = new HuoBiPong(webSocket);
		pool.submit(pong);
		
	}
	
	/**
	 * 从火币订阅消息
	 * @param subscribeStr	订阅的消息
	 */
	public void send(String subscribeStr) {
		this.webSocket.send(subscribeStr);
	}
	
	
	
	// 建立socket连接的时候调用
	public void onOpen(WebSocket webSocket, Response response) {
		log.info("链接了");
	}

	// 收到传递String类型参数时调用
	public void onMessage(WebSocket webSocket, String text) {
		log.info("收到消息了 ->" + text);
		JSONObject js = new JSONObject();
		if(text.contains("ping")){
			js.put("pong", JSONObject.parseObject(text).get("ping"));

		}
		webSocket.send(js.toJSONString());
	}

	// 接收ByteString类型数据的时候调用
	@SneakyThrows
	public void onMessage(WebSocket webSocket, ByteString bytes) {
		String text = new String(DataFormatUtil.decode(bytes.toByteArray()));
		log.info("收到消息了 ->" + text);

		if(text.contains("ping")){
			String pong = text.replace("ping", "pong");
			webSocket.send(pong);
			log.info("回复了pong ->" + pong);
		}


	}

	// 连接关闭的时候调用
	public void onClosing(WebSocket webSocket, int code, String reason) {
		rebooting();
		log.info("---------------onClosing连接关闭重连");
	}

	// 连接关闭后调用
	public void onClosed(WebSocket webSocket, int code, String reason) {
		rebooting();
		log.info("---------------onClosed连接关闭重连");
	}

	// 连接失败调用
	public void onFailure(WebSocket webSocket, Throwable t, Response response) {
		rebooting();
		log.info("---------------huobi连接异常重连");
	}

	private void rebooting(){

	}
}
